﻿/*!	main.cpp
	@brief	ゲームライブラリエントリー
Copyright (c) 2009 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "lib/window/r_window_entry.h"
#include "lib/window/r_window_initializer.h"
#include "lib/score/r_score_server.h"

#include "skeleton_score.h"

#include "lib/alloc/r_alloc_heap.h"
#include "lib/alloc/r_alloc_nedmalloc.h"
#include "lib/alloc/r_alloc_new_delete.h"
#include "lib/draw/r_draw_allocator.h"

#include "lib/render/r_render_allocator.h"
#include "lib/gui/r_gui_allocator.h"
#include "lib/develop/r_develop_allocator.h"
#include "lib/raytrace/r_raytrace_allocator.h"
#include "lib/stream/r_stream_allocator.h"
#include "lib/resource/r_resource_allocator.h"

#include "lib/resource/r_resource_server.h"
#include "lib/resource/texture/r_resource_loader_texture.h"

// とりあえずアロケータ
static	cRAllocNedmalloc*	defaultAllocator = NULL;

// 描画用アロケータ
static	cRAllocHeap*	drawAllocator = NULL;
// 文字列用アロケータ
static	cRAllocHeap*	stringAllocator = NULL;

// GUI用アロケータ
static	cRAllocHeap*	guiAllocator = NULL;

// 開発用アロケータ
static	cRAllocNedmalloc*	developAllocator = NULL;

// レイトレースアロケータ
static	cRAllocNedmalloc*	raytraceAllocator = NULL;

void r_window_entry()
{
	// アロケータを設定する
	defaultAllocator = new cRAllocNedmalloc( _RST ( "default" ), RALLOC_SIZE_MB( 128 ), 4 );

	drawAllocator = new cRAllocHeap( _RST ( "draw" ), RALLOC_SIZE_MB( 16 ), 4 );
	stringAllocator = new cRAllocHeap( _RST ( "string" ), RALLOC_SIZE_MB( 1 ), 4 );
	guiAllocator = new cRAllocHeap( _RST ( "gui" ), RALLOC_SIZE_MB( 1 ), 4 );
#if defined(RDEBUG_DEVELOP_BUILD)
	developAllocator = new cRAllocNedmalloc( _RST ( "develop" ), RALLOC_SIZE_MB( 128 ), 4 );
#else
	developAllocator = new cRAllocNedmalloc( _RST ( "develop" ), RALLOC_SIZE_MB( 1 ), 4 );
#endif

	raytraceAllocator = new cRAllocNedmalloc( _RST ( "raytrace" ), RALLOC_SIZE_MB( 64 ), 4 );

	// ストリーム用アロケータ
	r::stream::AllocatorData::setAllocator( defaultAllocator );
	// リソース用アロケータ
	r::resource::AllocatorData::setAllocator( defaultAllocator );
	// 描画用アロケータ
	r::draw::AllocatorData::setAllocator( drawAllocator );
	r::draw::StringAllocatorData::setAllocator( stringAllocator );
	// レンダーオブジェクト用アロケータ
	r::render::AllocatorData::setAllocator( drawAllocator );
	// GUI用
	r::gui::AllocatorData::setAllocator( guiAllocator );
	// 開発用
	r::develop::AllocatorData::setAllocator( developAllocator );
	// レイトレース
	r::raytrace::AllocatorData::setAllocator( raytraceAllocator );

	// リソースサーバ初期化
	r::resource::Server::create();
	// リソースサーバにローダを追加する
	r::resource::Server::instance resourceServer;
	{
		r::resource::texture::Loader::create();
		r::resource::texture::Loader::instance loader;
		resourceServer->addResourceLoader( &loader() );
	}

	cRWindowInitializer wininit;
	wininit.setAppName ( _RST ( "futhark" ) );
	wininit.setWindowSize ( 1280, 720 );

	cRScoreServer::instance score;
	score->jumpScore( new cSkeletonScore() );
}

void r_window_exit()
{
	r::resource::Server::destroy();
	r::resource::texture::Loader::destroy();

	delete raytraceAllocator;
	delete developAllocator;

	delete guiAllocator;
	delete stringAllocator;
	delete drawAllocator;

	delete defaultAllocator;
}

